<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
<%@ include file="/pub/headmeta.jsp" %>
</head>
<body class="edit-bgr-color">
	<div class="right-head"><span class="right-head-font">修改角色</span></div>
	<div align="center" style="width: 98%">	
		<form id="editForm" method="post" enctype="multipart/form-data">	
			<input type="hidden" id="roleId" name="roleId" value="${role.roleId}"/>		
			<table class="table_border" style="width: 600px">
				<tr>
					<td width="35%" align="right">角色名称：</td>
					<td width="65%"><input type="text" name="name" id="name" style="width: 80%" required="required" value="${role.name}"/>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>
				<tr>
					<td align="right">角色描述：</td>
					<td><textarea name="description" id="description" style="width:80%" rows="5">${role.description}</textarea>&nbsp;&nbsp;<font color='red'>*</font></td>					
				</tr>				
			</table>
			<br>
			<div align="left">分配权限：<input id="ckAll" type="checkbox" onclick="checkAll(this.checked,'cb');"/>全&nbsp;&nbsp;选</div>
			<hr width="100%"/>
			<table class="" id="MyTableList" border="0" cellpadding="0" cellspacing="1" style="width: 100%"></table>
			<div class="edit-line"></div>
			<input  type="button" class="button-1" value="修改" onclick="doEdit();"/>	
			<input  type="button" class="button-1" value="重置" onclick="resetEx(this.form);"/>					
			<input  type="button" class="button-1" value="返回" onclick="back();"/>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
<!--
var power;
//自动调用初始化
$(document).ready(function(){	
	var url="${ctx}/role!getOpsByRoleId.action";
	$.post(url, 'id=${role.roleId}', function callback(txt){ 
		var json = $.parseJSON(txt);
		if (json.code==0) {	
			power=json.info;				
			createTable(4);				
		}else{
			alert('根据角色ID获取模块分组操作失败！');
		}
	});
});


//编辑
function doEdit() {
	if ($("#name").val()== ""){
		alert('角色名称不能为空！');
		return;
	}
	if ($("#description").val() == ""){
		alert('角色描述不能为空！');
		return;
	}
	
	var form=$("#editForm");
	form.attr("action","${ctx}/role!edit.action"); 
	form.submit();		
}

//返回
function back(){
	location.href="${ctx}/role!viewList.action";
}

//数据处理 allOp-所有分组模块 ops-改角色拥有的权限
function dealData(allOp,ops){
	var records=new Array();	
	for(var i=0;i<allOp.length;i++){
		if(ops){			
			for(var j=0;j<ops.length;j++){
				if(allOp[i].operateId==ops[j].operateId){					
					allOp[i].isChecked=true;
					break;
				}
			}
		}
		var group=allOp[i].moduleName;	
		var flag=false;
		for(var n in records){
			if(records[n].group==group){
				records[n].ops.push(allOp[i]);
				flag=true;
				break;
			}
		}
		if(!flag){
			var obj=new Object();
			obj.group=group;
			var ops=new Array();
			ops.push(allOp[i]);
			obj.ops=ops;
			records[records.length]=obj;
		}			
	}
	return records;
}

//创建表数据 wrapNum-每行显示列数
function createTable(wrapNum){	
	//创建表
	var table=document.getElementById('MyTableList');
	for( var i=table.rows.length-1; i>=0; i--){
		table.deleteRow(i); 
	}
	var b=document.createElement("tbody");
	var p=0;
	var row;
	var w=100/wrapNum+"%";
	for(var att in power){	
		if(p%wrapNum==0){
			row=document.createElement("tr");	
			setTdStyle(row);b.appendChild(row);
		}	
		//内容准备
 		var content="<fieldset>";
 		content+="<legend><input type='checkbox' onclick=\"selectGroup(this.checked,'"+att+"');\" />"+att+"</legend>";
		for(var n in power[att]){
			var o=power[att][n];
			var sel=o.checked?"checked='checked'":"";
			content+="<p><input id='cb_"+o.operateId+"' name='cb' type='checkbox' "+sel+" value='"+o.operateId+"'/>"+o.operateName+"</p>";
		}			
 		content+="</fieldset>";	
 		//放入单元格
 		var c=document.createElement("td");	
		c.style.width=w;
		c.vAlign="top";
		c.innerHTML=content;
		row.appendChild(c);	
		p++;
	}
	table.appendChild(b);
}

//选择一组
function selectGroup(flag,att){
	for(var n in power[att]){
		var o=power[att][n];
		document.getElementById('cb_'+o.operateId).checked=flag;
	}
}
//-->
</script>
